package weaver.interfaces.ht.wlbmsq;

import java.io.File;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.apache.commons.codec.binary.Base64;

import jcifs.UniAddress;

public class SendMail_Util {

	public static String setHtml(String lcbh, ArrayList<Map<String, String>> list) {
		String s = "<div>\r\n" + "    您好：\r\n" + "</div>\r\n" + "<div>\r\n" + "    单号为：" + lcbh
				+ "的物料编码申请创建情况如下表：&nbsp;\r\n" + "</div>\r\n" + "<div>\r\n" + " \r\n" + "</div>\r\n" + "<div>\r\n"
				+ "<table cellpadding=\"0\" style=\"border-collapse: collapse; width: 682px; border-color: rgb(102, 102, 102); border-width: 1px; border-style: solid; height: 289px;\">\r\n"
				+ " <colgroup><col style=\"width: 99px;\">\r\n" + " <col style=\"width: 65px;\" span=\"9\">\r\n"
				+ " </colgroup><tbody><tr height=\"27\">\r\n"
				+ "  <td colspan=\"10\" class=\"et2\" height=\"27\" width=\"682\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">SAPCode创建明细</td>\r\n"
				+ " </tr>\r\n" + " <tr height=\"79\">\r\n"
				+ "  <td class=\"et4\" height=\"79\" width=\"99\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 700; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">序号</td>\r\n"
				+ "  <td class=\"et5\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 700; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">描述</td>\r\n"
				+ "  <td class=\"et5\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 700; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">型号</td>\r\n"
				+ "  <td class=\"et5\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 700; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">单位</td>\r\n"
				+ "  <td class=\"et5\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 700; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">Part No</td>\r\n"
				+ "  <td class=\"et5\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 700; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">Purchse Group</td>\r\n"
				+ "  <td class=\"et5\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 700; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">MTPY</td>\r\n"
				+ "  <td class=\"et5\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 700; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">Matl Group</td>\r\n"
				+ "  <td class=\"et5\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 700; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">Model NO</td>\r\n"
				+ "  <td class=\"et11\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 700; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">SAPCode</td>\r\n"
				+ " </tr>\r\n";

		for (int i = 0; i < list.size(); i++) {
			Map<String, String> map = list.get(i);
			s += " <tr>\r\n"
					+ "  <td class=\"et6\" height=\"79\" width=\"99\" x:num=\"1\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">"
					+ (i + 1) + "</td>\r\n"
					+ "  <td class=\"et7\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">"
					+ map.get("ms") + "</td>\r\n"
					+ "  <td class=\"et7\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">"
					+ map.get("xh") + "</td>\r\n"
					+ "  <td class=\"et7\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">"
					+ map.get("dw") + "</td>\r\n"
					+ "  <td class=\"et7\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">"
					+ map.get("gc") + "</td>\r\n"
					+ "  <td class=\"et7\" width=\"65\" x:num=\"303\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">"
					+ map.get("pg") + "</td>\r\n"
					+ "  <td class=\"et7\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">"
					+ map.get("mtpy") + "</td>\r\n"
					+ "  <td class=\"et7\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">"
					+ map.get("mg") + "</td>\r\n"
					+ "  <td class=\"et7\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">"
					+ map.get("mn") + "</td>\r\n"
					+ "  <td class=\"et12\" width=\"65\" x:str=\"\" style=\"padding-top: 1px; padding-left: 1px; padding-right: 1px; color: rgb(0, 0, 0); font-size: 16px; font-weight: 400; font-style: normal; text-decoration: none solid rgb(0, 0, 0); font-family: 宋体; border: 1px solid rgb(102, 102, 102);\" align=\"center\" valign=\"middle\">"
					+ map.get("sapcode") + "</td>\r\n" + " </tr>\r\n";
			;
		}

		s += "</tbody></table><br>\r\n" + "</div><div><font color=\"#ff0000\"><b>该邮件由系统自动发送，请勿回复</b></font></br></br></div>";

		return s;
	}

	public static void sendMail(String body,String subject,String mail) throws AddressException, MessagingException {

		Properties prop = new Properties();
		prop.setProperty("mail.transport.protocol", "smtp"); // 协议
		prop.setProperty("mail.smtp.host", "172.27.3.94"); // 主机名
		prop.setProperty("mail.smtp.auth", "true"); // 是否开启权限控制
		prop.setProperty("mail.debug", "true"); // 返回发送的cmd源码
		Session session = Session.getInstance(prop);
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("hioffice@hitechsemi.com")); // 自己的email
//		msg.setRecipient(RecipientType.TO, new InternetAddress("Jia.Liu@hitechsemi.com")); // 要发送的email，可以设置数组

		msg.setRecipient(RecipientType.TO, new InternetAddress(mail)); // 要发送的email，可以设置数组
		msg.setSubject(subject); // 邮件标题
//		String body = "";
		Multipart mainPart = new MimeMultipart();
		// 创建一个包含HTML内容的MimeBodyPart
		BodyPart html = new MimeBodyPart();
		// 设置HTML内容
		html.setContent(body, "text/html; charset=utf-8");
		mainPart.addBodyPart(html);
		// 将MiniMultipart对象设置为邮件内容
		msg.setContent(mainPart);
//		msg.setText();//邮件正文
		// 不被当作垃圾邮件的关键代码--Begin ，如果不加这些代码，发送的邮件会自动进入对方的垃圾邮件列表
		msg.addHeader("X-Priority", "3");
		msg.addHeader("X-MSMail-Priority", "Normal");
		msg.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); // 本文以outlook名义发送邮件，不会被当作垃圾邮件
		msg.addHeader("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
		msg.addHeader("ReturnReceipt", "1");
		// 不被当作垃圾邮件的关键代码--end
		Transport trans = session.getTransport();
		trans.connect("hioffice", "Ht@123"); // 邮件的账号密码
		trans.sendMessage(msg, msg.getAllRecipients());

	}
}
